package com.itheima.redis;

import com.itheima.redis.util.SingleRedisLock;
import com.itheima.redis.util.SingleRedisLockDemo;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class LockTestDemo {

    static JedisPool jedisPool =new JedisPool("106.53.254.147",6379);

    public static void main(String[] args) {

        //模拟多个5个客户端
        for (int i=0;i<5;i++) {
            Thread thread = new Thread(new LockRunnable());
            thread.start();
        }

//        Jedis jedis = jedisPool.getResource();
//        String demo = jedis.set("demo", "1", SetParams.setParams().nx().px(20000));
//        System.out.println(demo);
    }

    private static class LockRunnable implements Runnable {
        @Override
        public void run() {

            SingleRedisLockDemo singleRedisLock = new SingleRedisLockDemo();

            String requestId = UUID.randomUUID().toString();

            boolean lockResult = singleRedisLock.addLock("lock");

            if (lockResult){

                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            singleRedisLock.replaceLock("lock");
        }
    }
}
